using System;
using System.Drawing;
using System.Collections.Generic;
using System.IO;
using DataDynamics.ActiveReports;
using GDFlacTool.Common;

namespace GDFlacTool.Forms
{
    public partial class Report : ActiveReport3
    {
        private readonly IList<Track> _tracks;
        private int index;

        public Report(IList<Track> tracks)
        {
            InitializeComponent();

            _tracks = tracks;

            DataSource = tracks;
        }

        private void Report_DataInitialize(object sender, EventArgs e)
        {
            Fields.Add("Art");
            Fields.Add("Disc");
            Fields.Add("Track");
            Fields.Add("Artist");
            Fields.Add("Album");
            Fields.Add("Year");
            Fields.Add("AlbumArtist");
        }

        private void Report_FetchData(object sender, FetchEventArgs eArgs)
        {
            if(index < _tracks.Count)
            {
                eArgs.EOF = false;

                if(_tracks[index].ArtByteArray != null)
                {
                    var ms = new MemoryStream(_tracks[index].ArtByteArray);

                    Image img = Image.FromStream(ms);

                    Fields["Art"].Value = img;

                    lblImageSize.Text = string.Format("Size: {0} X {1}", img.Width, img.Height);
                }
                else
                {
                    Fields["Art"].Value = Properties.Resources.Empty;
                    lblImageSize.Text = "Size:";
                }

                if(string.IsNullOrEmpty(_tracks[index].DiscNumber))
                {
                    Fields["Disc"].Value = "##!";
                    txtDisc.ForeColor = Color.Red;
                }
                else
                {
                    Fields["Disc"].Value = _tracks[index].DiscNumber;
                    txtDisc.ForeColor = Color.Black;
                }

                if (string.IsNullOrEmpty(_tracks[index].TrackNumber))
                {
                    Fields["Track"].Value = "##!";
                    txtTrack.ForeColor = Color.Red;
                }
                else
                {
                    Fields["Track"].Value = _tracks[index].TrackNumber;
                    txtTrack.ForeColor = Color.Black;
                }

                if(string.IsNullOrEmpty(_tracks[index].Artist))
                {
                    txtArtist.ForeColor = Color.Red;
                    Fields["Artist"].Value = "#Missing!";
                }
                else
                {
                    txtArtist.ForeColor = Color.Black;
                    Fields["Artist"].Value = _tracks[index].Artist;
                }

                if (string.IsNullOrEmpty(_tracks[index].Album))
                {
                    txtAlbum.ForeColor = Color.Red;
                    Fields["Album"].Value = "#Missing!";
                }
                else
                {
                    txtAlbum.ForeColor = Color.Black;
                    Fields["Album"].Value = _tracks[index].Album;
                }

                if(string.IsNullOrEmpty(_tracks[index].Year))
                {
                    txtYear.ForeColor = Color.Red;
                    Fields["Year"].Value = "##!";
                }
                else
                {
                    txtYear.ForeColor = Color.Black;
                    Fields["Year"].Value = _tracks[index].Year;
                }

                if (string.IsNullOrEmpty(_tracks[index].Albumartist))
                {
                    txtAlbumArtist.ForeColor = Color.Red;
                    Fields["AlbumArtist"].Value = "#Missing!";
                }
                else
                {
                    txtAlbumArtist.ForeColor = Color.Black;
                    Fields["AlbumArtist"].Value = _tracks[index].Albumartist;
                }
            }
            else
            {
                eArgs.EOF = true;
            }

            index++;
        }
    }
}
